<?php
namespace app\admin\model;

use think\Model;  
use think\Loader;
use think\Validate;
use think\Db;
class BannerM  extends Model
{

  protected  $pk='bitem_id';//主键
  protected $table = 'xcx_banner_item';
    public function add_bannerM($data)
    {
    	//halt($data);
    	//yanzheng
    	$validate = Loader::validate('BannerM');
    	if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}
		$http='http';
		$img['form']=0;
		$img['img_url']=$data['bannerm_img'];
		if(stristr($img['img_url'],$http))
		{
			$img['form']=1;
		}
		$img['createtime']=time();
		$img['img_states']=1;
		$getImgId=db('bannerimg')->insertGetId($img);
        $arr['banimg_id']=$getImgId;
		$arr['bitem_name']=$data['bannerm_name'];
		/*判断跳转到哪*/
	    $arr['action_url']=$data['action'];
	    $arr['banner_id']=$data['banner_id'];
	    $arr['bitem_sort']=$data['bannerm_sort'];
	    $arr['bitem_states']=1;
	    $arr['createtime']=time();

		//判断名称是否唯一
		$isNotRepeat=$this->where('bitem_name',$data['bannerm_name'])->find();
		//halt($type_yan);
		if($isNotRepeat['bitem_id']!="")
		{
			 return ['valid'=>0,'msg'=>'名称重复'];
			 exit;
		}

		//halt($user_yan);
		$result = $this->save($arr);
		if(false === $result){
		    // 验证失败 输出错误信息
		    return ['valid'=>1,'msg'=>'添加失败'];
		}else{
			//添加操作记录
			return ['valid'=>4,'msg'=>'添加成功'];
		}
    }
}   	